package cvi.com.reader.adapter;

import android.content.Context;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.bean.FlagBean;
import cvi.com.reader.bean.MusicListDataBean;
import cvi.com.reader.bean.constant;

/**
 * Created by 梅涛 on 2020/7/24.
 */

public class MusicListAdapter extends BaseAdapter {
    private Context context;
    private List<MusicListDataBean.DataBean> dataBeanList;
    private PlayerClickInterface onPlayerClickInterface;
    private boolean isPlaying = false;
    private String strUrl;
    private int categoriesId;
    private MediaPlayer mediaPlayer;
    private Button preBtn;
    private MusicListDataBean.DataBean preBean;

    public PlayerClickInterface getOnPlayerClickInterface() {
        return onPlayerClickInterface;
    }

    public void setOnPlayerClickInterface(PlayerClickInterface onPlayerClickInterface) {
        this.onPlayerClickInterface = onPlayerClickInterface;
    }

    public MusicListAdapter(Context context, List<MusicListDataBean.DataBean> dataBeanList, int categoriesId) {
        this.context = context;
        this.dataBeanList = dataBeanList;
        this.categoriesId = categoriesId;
    }

    @Override
    public int getCount() {
        return dataBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder viHolder;
        final MusicListDataBean.DataBean dataBean = dataBeanList.get(i);
        final FlagBean flagBean = new FlagBean();
        if (view == null) {
            view = LayoutInflater.from(context).inflate(
                    R.layout.item_music_list, null);
            viHolder = new ViewHolder(view);

            view.setTag(viHolder);
        } else {
            viHolder = (ViewHolder) view.getTag();
        }
        if (dataBean.isPlaying()) {
            viHolder.btnPlay.setBackgroundResource(R.mipmap.ic_pase);
        } else {
            viHolder.btnPlay.setBackgroundResource(R.mipmap.ic_play);
        }
        if (Hawk.contains("flagBean")) {
            FlagBean flagBean1 = Hawk.get("flagBean");
            if (categoriesId == flagBean1.getCategoriesId() && dataBean.getId() == flagBean1.getMusicId()) {
                viHolder.btnPlay.setBackgroundResource(R.mipmap.ic_pase);
            } else {
                viHolder.btnPlay.setBackgroundResource(R.mipmap.ic_play);

            }
        }

        MusicListDataBean.DataBean.BgmInfoBean bgm_info = dataBean.getBgm_info();
        viHolder.musicName.setText(dataBean.getTitle());
        viHolder.songer.setText(dataBean.getAuthor());
        viHolder.time.setText(bgm_info.getDuration());
      /*  if (Hawk.contains("ip")) {
            strUrl = Hawk.get("ip");
        }*/

        viHolder.goUse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onPlayerClickInterface.onApplyMusic("http://" +  constant.URL + dataBean.getMedia_path(), dataBean.getId(), dataBean.getTitle(), dataBean.getAuthor());
            }
        });
        Log.d("dsssssssssss", strUrl + dataBean.getMedia_path());
        viHolder.btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Button button = (Button) view;
                if (dataBean.isPlaying()) {//正在播放
                    button.setBackgroundResource(R.mipmap.ic_play);
                    onPlayerClickInterface.onPauseClick("http://" + strUrl + dataBean.getMedia_path());
                    Log.d("dsffff", "111111==" + dataBean.getId());
                    dataBean.setPlaying(false);
                } else {//未在播放
                    if (preBean != null) {//有前一个播放的音频，先暂停
                        preBean.setPlaying(false);
                        onPlayerClickInterface.onPauseClick("http://" + strUrl + preBean.getMedia_path());
                    }
                    if (preBtn != null) {
                        preBtn.setBackgroundResource(R.mipmap.ic_play);
                    }
                    onPlayerClickInterface.onPlayerClick("http://" + strUrl + dataBean.getMedia_path());
                    button.setBackgroundResource(R.mipmap.ic_pase);
                    Log.d("dsffff", "22222==" + dataBean.getId());
                    dataBean.setPlaying(true);
                    flagBean.setCategoriesId(categoriesId);
                    flagBean.setMusicId(dataBean.getId());
                    Hawk.put("flagBean", flagBean);
                    preBtn = button;
                    preBean = dataBean;
                }
            }
        });
        return view;
    }

    public interface PlayerClickInterface {
        //播放监听
        public void onPlayerClick(String url);

        //暂停监听
        public void onPauseClick(String url);

        public void onApplyMusic(String url, int musicId, String musicName, String author);
    }


    final static class ViewHolder {
        @BindView(R.id.btn_play)
        Button btnPlay;
        @BindView(R.id.music_name)
        TextView musicName;
        @BindView(R.id.songer)
        TextView songer;
        @BindView(R.id.go_use)
        Button goUse;
        @BindView(R.id.time)
        TextView time;
        @BindView(R.id.bb)
        RelativeLayout bb;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
